package IC.Tables.Types;

import java.util.*;

/**
 * Method Type
 * Contains the input parameter types and output type
 */
public class MethodType extends Type {
	private List<Type> paramTypes;
	private Type returnType;
	
	public MethodType(Type returnType, List<Type> paramTypes){
		super(null);
		this.returnType = returnType;
		this.paramTypes = paramTypes;
	}
	
	/**
	 * getters
	 */
	public Type getReturnType(){
		return this.returnType;
	}
	
	public List<Type> getParamTypes(){
		return this.paramTypes;
	}

	/**
	 * checks if a MethodType mt equals this 
	 * (checks its name, its returned type and all input parameter types)
	 */
	public boolean equals(MethodType mt){
		if (this.getName() != mt.getName()) return false;
		else if (this.returnType != mt.getReturnType()) return false;
		else{
			Iterator<Type> myIter = this.paramTypes.iterator();
			Iterator<Type> otherIter = mt.paramTypes.iterator();
			
			while (myIter.hasNext() && otherIter.hasNext()){
				if (myIter.next() != otherIter.next()) return false; 
			}
			if (myIter.hasNext() || otherIter.hasNext()) return false;
			else return true;
		}
	}
	
	/**
	 * returns the string representation for method type
	 */
	public String toString(){
		String str = "{";

		// parameter types
		Iterator<Type> paramIter = paramTypes.iterator();
		if (paramIter.hasNext()) str += paramIter.next().getName(); 
		while (paramIter.hasNext()) str += ", "+paramIter.next().getName(); 
		
		// return type
		str += " -> "+this.returnType.getName()+"}";
		
		return str;
	}
}